package com.bjsxt.shop.back.springmvc.controller;

import java.util.Date;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.bjsxt.common.util.ConstatFinalUtil;
import com.bjsxt.shop.common.controller.BaseController;
import com.bjsxt.shop.goods.service.IStatService;

@Controller
@RequestMapping("/back/stat")
public class StatBackController extends BaseController
{
	@Resource
	private IStatService statService;
	
	@RequestMapping("/goodsList")
	public String goodsList(String st,String ed,Model model,HttpServletRequest request,HttpServletResponse response)
	{
		ConstatFinalUtil.SYS_LOGGER.info("==goodsList==");
		/* 取数据 */
		this.goodsListJSON(request,st, ed, model);
		return "/back/stat/goodsList" ; 
	}

	@RequestMapping(value = "/goodsListJSON",produces="text/html;charset=UTF-8")
	@ResponseBody
	private String goodsListJSON(HttpServletRequest request, String st, String ed, Model model)
	{
		JSONObject resultJSON = new JSONObject();
		if(st==null)
		{
			st="";
		}
		if(ed==null)
		{
			ed="";
		}
		Date stDate=null;
		Date edDate=null;
		if(!"".equalsIgnoreCase(st)&&!"".equalsIgnoreCase(ed))
		{
			stDate=this.dateFormatUtil.strDateTime(st);
			edDate=this.dateFormatUtil.strDateTime(ed);
		}
		Map<String, Object> condMap=new HashMap<String, Object>();
		condMap.put("stDate", stDate);
		condMap.put("edData", edDate);
		List<Map> statList=this.statService.statGoodsMapService(condMap);
		
		model.addAttribute("statList", statList);
		model.addAttribute("st", st);
		model.addAttribute("ed", ed);
		
		resultJSON.put("total", statList.size());
		
		JSONArray rowsArr=new JSONArray();
		for (Iterator iterator = statList.iterator(); iterator.hasNext();)
		{
			Map map = (Map) iterator.next();
			JSONObject rowJSON=new JSONObject();
			rowJSON.put("createTime", map.get("createTime"));
			rowJSON.put("count", map.get("count"));
			
			rowsArr.add(rowJSON);
		}
		
		resultJSON.put("rows", rowsArr);
		return resultJSON.toJSONString(); 
 		
	}
}
